package com.cet.lxf.collection.test01;

import java.util.Arrays;
import java.util.List;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-08-03 11:18
 **/
public class Main {
    public static void main(String[] args) {
        List<Integer> list = List.of(1, 2, 3);

        // List转Array

        // toArray
        Object[] array1 = list.toArray();
        // toArray(T[])
        Integer[] array2 = list.toArray(new Integer[5]); // 数组过大，填充null
        Number[] array3 = list.toArray(new Number[0]);  //数组过小，自动创建一个新的刚好够大的数组
        // default <T> T[] toArray(IntFunction<T[]> generator)
        Integer[] array4 = list.toArray(Integer[]::new);
        Arrays.stream(array1).forEach(System.out::println);
        Arrays.stream(array2).forEach(System.out::println);
        Arrays.stream(array3).forEach(System.out::println);
        Arrays.stream(array4).forEach(System.out::println);

        // Array转List
        List<Integer> list1 = List.of(array4);

        List<Integer> list2 = Arrays.asList(array4);
        for (int i = 0; i < list2.size(); i++) {
            list2.set(i,list2.get(i) + 1);
        }

        list1.forEach(System.out::println);

        list2.forEach(System.out::println);

    }
}
